#include <jni.h>
#include <stdio.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavfilter/avfilter.h"
#include "ffmpeg.h"

static JavaVM *jvm = NULL;
//java虚拟机
static jclass m_clazz = NULL;

/*
 * Class:     solid_ren_ffmpeg_FFmpegInfo
 * Method:    avcodecinfo
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_xgkj_ffmpegandroiddemo_FFmpegUtils_avcodecInfo
        (JNIEnv *env, jobject jclass) {
    char info[40000] = {0};

    av_register_all();

    AVCodec *c_temp = av_codec_next(NULL);

    while (c_temp != NULL) {
        if (c_temp->decode != NULL) {
            sprintf(info, "%s[Dec]", info);
        } else {
            sprintf(info, "%s[Enc]", info);
        }
        switch (c_temp->type) {
            case AVMEDIA_TYPE_VIDEO:
                sprintf(info, "%s[Video]", info);
                break;
            case AVMEDIA_TYPE_AUDIO:
                sprintf(info, "%s[Audio]", info);
                break;
            default:
                sprintf(info, "%s[Other]", info);
                break;
        }
        sprintf(info, "%s[%10s]\n", info, c_temp->name);


        c_temp = c_temp->next;
    }
    //LOGE("%s", info);

    return (*env)->NewStringUTF(env, info);
}


JNIEXPORT jint JNICALL Java_com_xgkj_ffmpegandroiddemo_FFmpegUtils_videotranscode
        (JNIEnv *env, jobject thiz, jint cmdnum, jobjectArray  cmdline){
    int argc = (*env)->GetArrayLength(env, cmdline);
    char *argv[argc];

//    LOGD("Kit argc %d\n", argc);
    int i;
    for (i = 0; i < argc; i++) {
        jstring js = (jstring) (*env)->GetObjectArrayElement(env, cmdline, i);
        argv[i] = (char*) (*env)->GetStringUTFChars(env, js, 0);
//        LOGD("Kit argv %s\n", argv[i]);
    }
    return run(argc, argv);
}

//int run(int argc, char **argv);

